package com.java2007.web.controller;

import com.java2007.pojo.ReaderInfo;
import com.java2007.service.AdminService;
import com.java2007.service.ReaderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author zy
 * @version V1.0
 * @date 2021/03/09
 */
@Controller
@Slf4j
public class AdminController {
    /**
     * 显示后台管理的导航栏
     * @return
     */
    @RequestMapping("/admin_header.html")
    public String adminHeader(){
        return "admin_header";
    }

    /**
     *显示后台的主页面
     * @return
     */
    @GetMapping("/admin_main.html")
    public String adminMain(){
        return "admin_main";
    }

    @Autowired
    private ReaderInfoService readerInfoService;

    @Autowired
    private AdminService adminService;

    /**
     * 后台管理显示全部读者信息
     * @return
     */
    @GetMapping("/readers/list")
    public ModelAndView list(){
        List<ReaderInfo> readerInfoList = readerInfoService.findAll();
        ModelAndView mav = new ModelAndView("admin_readers");
        mav.addObject("readerInfoList",readerInfoList);
        return mav;
    }

    /**
     * 显示后台管理增加读者的页面
     * @param readerInfo
     * @return
     */
    @GetMapping("/readers/addui")
    public ModelAndView addUI(ReaderInfo readerInfo){
        ModelAndView mav = new ModelAndView("admin_reader_add");
        return mav;
    }

    /**
     * 后台管理同时增加读者信息及读者卡信息
     * @param readerInfo
     * @return
     */
    @PostMapping("/readers/add")
    public ModelAndView add(ReaderInfo readerInfo){
        log.info("add readerInfo->{}", readerInfo);

        readerInfoService.add(readerInfo);
        ModelAndView mav = new ModelAndView("redirect:/readers/list");
        mav.addObject("succ","增加成功");
        return mav;
    }

    /**
     * 后台管理删除读者信息及读者卡信息
     * @param readerId
     * @return
     */
    @GetMapping({"/readers/delete.html"})
    public ModelAndView delete(Long readerId){
        log.info("ReaderInfoController delete....{}", readerId);
        readerInfoService.delete(readerId);
        ModelAndView mav = new ModelAndView("redirect:/readers/list");
        mav.addObject("succ","删除成功");
        return mav;
    }

    /**
     * 后台管理：到更新密码界面
     * @param readerId
     * @return
     */
    @GetMapping("/reader/repasswd")
    public ModelAndView readerRepasswd(Long readerId){
        ModelAndView mav=new ModelAndView("admin_repasswd");
        return mav;
    }

    /**
     * 更新密码
     * @param
     * @return
     */
    @PostMapping("/reader/updateAdminPwd")
    public ModelAndView updatePwd(String reNewPasswd, Long adminId){
        log.info("add reNewPasswd->{}", reNewPasswd);
        log.info("add readerId->{}", adminId);
        adminService.updatePwd(reNewPasswd,adminId);
        ModelAndView mav=new ModelAndView("admin_repasswd");
        return mav;
    }

    /**
     * 后台管理：回显选中读者的相关信息
     * @param readerId
     * @return
     */
    @GetMapping("/readers/edit.html")
    public ModelAndView updateUI(Long readerId){
        ReaderInfo readerInfo = readerInfoService.findByReaderId(readerId);
        ModelAndView mav = new ModelAndView("admin_reader_edit");
        mav.addObject("readerInfo",readerInfo);
        return mav;
    }

    /**
     * 后台管理：编辑选中图书的相关信息
     * @param readerInfo
     * @return
     */
    @PostMapping("/readers/edit.html")
    public ModelAndView edit(ReaderInfo readerInfo){
        log.info("edit readerInfo->{}", readerInfo);
        ModelAndView mav = new ModelAndView("redirect:/readers/list");
        readerInfoService.edit(readerInfo);
        mav.addObject("succ","更新成功");
        return mav;
    }
}
